package com.bennu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bennu.Dto.DishDto;
import com.bennu.common.CustomException;
import com.bennu.domain.Dish;
import com.bennu.domain.DishFlavor;
import com.bennu.mapper.DishMapper;
import com.bennu.service.DishFlavorService;
import com.bennu.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存基本信息到dish
        this.save( dishDto );
        //菜品id
        Long dishId = dishDto.getId();
        //菜品口味
        /**
         * 将flavors遍历获取其中的Dishid，之后将菜品的dishid赋给获取出来的flavors中的Dishid
         */
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map( (item)->{
            item.setDishId( dishId );
            return item;
        } ).collect( Collectors.toList() );
      /*  for (DishFlavor item : flavors) {
            item.setDishId( dishId );

        }*/

        //菜品口味信息保存到dish_flavor
         dishFlavorService.saveBatch( flavors );
    }
    //根据id查询菜品信息和口味信息
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish byId = this.getById( id );
        //拷贝
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties( byId,dishDto );
        //查询口味信息dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq( DishFlavor::getDishId,byId.getId() );
        List<DishFlavor> flavors = dishFlavorService.list( queryWrapper );
        dishDto.setFlavors( flavors );
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表信息
        this.updateById( dishDto );
        //清理当前菜品对应口味数据dish_flavor，delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq( DishFlavor::getDishId,dishDto.getId() );
        dishFlavorService.remove( queryWrapper );
        //添加提交过来的口味数据dish_flavor，insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map( (item)->{
            item.setDishId( dishDto.getId() );
            return item;
        } ).collect( Collectors.toList() );
        dishFlavorService.saveBatch( flavors );
    }
    //删除菜品
    @Override
    public void removeId(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        //查询当前状态是否起售，如果起售不能删除
        queryWrapper.in( Dish::getId,ids );
        queryWrapper.eq( Dish::getStatus,1 );
        int count = (int) this.count(queryWrapper);
        if ( count>0 ){
            throw new CustomException( "当前状态起售不能删除" );
        }
        //如果可以删除，删除菜品信息
        this.removeByIds( ids );
        //删除菜品口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        //执行删除口味。
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
    }


}
